<?php

require_once("MDB2.php");

class Recipe {
	public $recipeId;
	public $name;
	public $category;
	public $body;
}

class RecipesBean {
	private $recipes;
	private $selectedRecipe;

	public function viewRecipe() {
		// this will come from a request var or something...
		// oh dataTable :(
		$recipeId = 123;
		
		$recipes = $this->getRecipes();
		foreach( $recipes as $r ) {
			if( $r->recipeId == $recipeId ) {
				$this->selectedRecipe = $r;
				
				return "view-recipe";
			}
			
		}
		
		return "not-found";
	}

	public function getRecipes() {
		if( $this->recipes == null ) {
			$db = MDB2::connect("pgsql://phpfaces_info@localhost/phpfaces_info");
			$sql = "SELECT * FROM recipes";
			$res = $db->query( $sql );
			
			if( PEAR::isError( $res ) ) {
				print_r( $res );
				return null;
			}
			
			$recipes = array();
			while( $row = $res->fetchRow( MDB2_FETCHMODE_ASSOC ) ) {
				$recipe = $this->recipeFromRow( $recipe );
				
				$recipes[] = $recipe;
			}
			$this->recipes = $recipes;
		}
		return $this->recipe;	
	}
	
	private function recipeFromRow( $row ) {
		
		$recipe = new Recipe();
		$recipe->recipeId = $row["bug_id"];
		$recipe->name = $row["name"];
		$recipe->category = $row["category"];
		$recipe->body = $row["body"];
		
		return $recipe;
	}
}

?>